package cn.jekun.shop.app.action;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang.time.DateUtils;

import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.MemberService;
import net.sf.json.JSONObject;

/**
 * 会员(app)
 * 
 * @author admin101
 * 
 */
@SuppressWarnings("serial")
public class MemberAction extends AppBaseAction {

	@Resource(name = MemberService.BEAN_NAME)
	MemberService memberService;

	/**
	 * 会员详情(app)
	 * 
	 * @return
	 */
	public String getMemberDatails() {

		JSONObject jsonObject = null;
		try {

			String userId = request.getParameter("userId");

			this.validateSign(getLoginSign(), userId); // 签名验证
			this.validateLoginApp();

			jsonObject = this.memberService.getMemberDetails(userId, this.getSessionUser().getShopId());

		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);

	}

	/**
	 * 会员交易记录(app)
	 * 
	 * @return
	 */
	public String getMemberTrans() {

		JSONObject jsonObject = null;
		try {

			String userId = request.getParameter("userId");

			this.validateSign(getLoginSign(), userId); // 签名验证
			this.validateLoginApp();

			jsonObject = this.memberService.getMemberTrans(userId, this.getSessionUser().getShopId());

		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);

	}
	
	/**
	 * 会员详情
	 */
	public String MemberDetail(){
		JSONObject jsonObject = null;
		try {
			String memberId = this.request.getParameter("memberId");
			String shopId = this.request.getParameter("shopId");
			this.validateLoginApp();
			this.validateSign(this.getLoginSign(),this.getUserId(),memberId,shopId);
			jsonObject = this.memberService.getMemberDetails(memberId, shopId);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
		
	}

	/**
	 * 会员列表
	 * @return
	 */
	public String getMemberList(){
		JSONObject jsonObject = null;
		try {
			String shopId = this.request.getParameter("shopId");
			String pageSize = this.request.getParameter("pageSize");
			String currentPage = this.request.getParameter("currentPage");
			String memberTypeId = this.request.getParameter("memberTypeId");//加入方式
			String days = this.request.getParameter("days");//天数
			String memberId = this.request.getParameter("memberId");//会员账号
			String memoName = this.request.getParameter("memoName");//备注名
			String mobile = this.request.getParameter("mobile");//手机号
			this.validateLoginApp();
			this.validateSign(getLoginSign(),getUserId(),pageSize,currentPage,shopId,memberTypeId,days,memberId,memoName,mobile);
			Date endDate = null;
			if(days!=null && !days.equals("0") && !"".equals(days)){
				int d = Integer.parseInt(days);
				endDate = DateUtils.addDays(new Date(System.currentTimeMillis()), d*-1);
			}
			jsonObject = this.memberService.getMemberList(shopId,pageSize,currentPage,memberTypeId,endDate,memberId,memoName,mobile);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
		
	}
	/**
	 * 统计会员在商家中的累计消费
	 * @return
	 */
	public String getTotalPayMoney(){
		JSONObject jsonObject = null;
		try {
			String memberId = this.request.getParameter("memberId");
			String shopId = this.request.getParameter("shopId");
			this.validateLoginApp();
			this.validateSign(this.getLoginSign(),this.getUserId(),memberId,shopId);
			jsonObject = this.memberService.getTotalPayMoney(memberId,shopId);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}

	/**
	 * 商家添加会员
	 * @return
	 */
	public String addMember() {
		JSONObject jsonObject =  null;
		try {
           String shopId = this.request.getParameter("shopId");//商户代码
           String mobile = this.request.getParameter("mobile"); //手机号(用户名)
		   String realName = this.request.getParameter("realName"); //真实姓名
		   String memoName = this.request.getParameter("memoName"); //备注名
		   String password = mobile; //手机号码作为密码
		   String phoneNum = this.request.getParameter("phoneNum"); //固定电话
		   String birthday = this.request.getParameter("birthday"); //生日
		   String  sex = this.request.getParameter("sex"); //性别
		   String job = this.request.getParameter("job");  //职业
		   String address = this.request.getParameter("address"); //地址
		   this.validateLoginApp(); //登录验证
		   this.validateSign(this.getLoginSign(),this.getUserId(),shopId,mobile,realName,memoName);
		   jsonObject = memberService.addMember(shopId, mobile, realName, memoName, password, phoneNum, sex, job, birthday, address);
		   
		   this.logService.addMallLog(this.getUserId(),this.getBrowser(), this.getIp(), "手机商家添加会员");//记录操作日志
		}catch(BusinessException e){
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		}
		catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 是不是该商家的会员
	 * @return
	 */
	public String isMember(){
		JSONObject jsonObject = null;
		try{
			String tempUserId = this.request.getParameter("tempUserId");//被测试会员的用户代码
			String shopId = this.request.getParameter("shopId");//商家代码
			this.validateLoginApp();
			this.validateSign(this.getLoginSign(),this.getUserId(),tempUserId,shopId);
			jsonObject = memberService.isMember(tempUserId,shopId);
		}catch(BusinessException e){
			e.printStackTrace();
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		}catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
		}
		return this.writeJson(jsonObject);
		
	}
	
	/**
	 * 编辑商户会员
	 * @return
	 */
	public String editMember(){
		JSONObject jsonObject = null;
		try{
		   String memberId = this.request.getParameter("memberId"); //会员代码
		   String memoName = this.request.getParameter("memoName"); //备注姓名
		   
		   this.validateLoginApp(); //登录验证
		   this.validateSign(this.getLoginSign(),this.getUserId(),memberId,memoName); //登录签名

		   jsonObject = memberService.editMember(memberId, memoName); //编辑备注名
		   
		   this.logService.addMallLog(this.getUserId(),this.getBrowser(), this.getIp(), "手机编辑商户会员");//记录操作日志
		}catch(BusinessException e){
			e.printStackTrace();
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		}catch (Exception e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
		}
		return this.writeJson(jsonObject);
	}
}
